/**
 * Created by 1 on 2016/5/16.
 */
// index page

var mongoose = require('mongoose');

var fs = require('fs');
var path = require('path');
var mainPath = path.join(__dirname, '../../');
var News = mongoose.model('News');
var Honor = mongoose.model('Honor');
var Certificate = mongoose.model('Certificate');
var Home = mongoose.model('Home');
var CompanyInformation = mongoose.model('Information');
var Recruit = mongoose.model('Recruit');
var Apply = mongoose.model('Apply');
var Quit = mongoose.model('Quit');
var User = mongoose.model('User');
var Feedback = mongoose.model('Feedback');
var Carousel = mongoose.model('Carousel');

var Video = mongoose.model('Video');
var Cultrue = mongoose.model('Cultrue');
// 首页
exports.admin = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/index', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};
exports.add_admin1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_admin', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};
exports.add_news1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_news', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.add_company_information1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }

  res.render('admin/add_company_information', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};
exports.edit_news1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  News.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_news', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};

exports.edit_company_information1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  CompanyInformation.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_company_information', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};
exports.news_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/news_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.company_news_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/company_news_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};
// 编辑轮播图排序
exports.edit_carouselSort = function(req, res) {
  var id = req.body._id;
  var sort = req.body.sort;
  console.log(req.body)
  var data = {
    sort: sort,
  }
  Carousel.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};
exports.carousel = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/carousel', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};
exports.video = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/video', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};
//获取轮播图
exports.get_carousel = function(req, res) {
  Carousel.find({}).sort('-_id').exec(function(err, datas) {
    if(err) {
      console.log('获取数据失败');
      var data = {
        status: 'failed',
      };
      res.json(data);
    } else {
      res.json({
        carousel: datas
      });
    }
  });
};
//获取视频
exports.get_video = function(req, res) {
  Video.find({}).sort('-_id').exec(function(err, datas) {
    if(err) {
      console.log('获取数据失败');
      var data = {
        status: 'failed',
      };
      res.json(data);
    } else {
      res.json({
        video: datas
      });
    }
  });
};
//删除一个轮播图
exports.del_carousel = function(req, res) {
  var id = req.body.id;
  Carousel.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      var delpath = mainPath + '/public/'+data.url;
      fs.exists(delpath, function(exists) {
        if(exists) {
          fs.unlinkSync(delpath);
          Carousel.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });
        } else {
          res.json({
            "status": "找不到文件"
          })
        }
      });
    }
  })
  

};
//删除一个视频
exports.del_video = function(req, res) {
  var id = req.body.id;
  Video.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });

};
// 添加新闻
exports.add_news = function(req, res) {
  var title = req.body.title;
  var time = req.body.time;
  var from = req.body.from;
  var category = req.body.category;
  var content = req.body.content;

  var data = new News({
    title: title,
    time: time,
    from: from,
    category: category,
    content: content
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};

// 添加企业信息
exports.add_company_information = function(req, res) {
  var title = req.body.title;
  var time = req.body.time;
  var from = req.body.from;
  var category = req.body.category;
  var content = req.body.content;

  var data = new CompanyInformation({
    title: title,
    category: category,
    content: content
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 编辑新闻
exports.edit_news = function(req, res) {
  var id = req.body._id;
  var title = req.body.title;
  var from = req.body.from;
  var category = req.body.category;
  var content = req.body.content;
  var data = {
    title: title,
    from: from,
    category: category,
    content: content
  }
  News.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};

// 编辑企业信息
exports.edit_company_information = function(req, res) {
  var id = req.body._id;
  var title = req.body.title;
  var category = req.body.category;
  var content = req.body.content;
  console.log(id)
  var data = {
    title: title,
    category: category,
    content: content
  }

  CompanyInformation.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};
// 分页获取新闻
exports.get_news = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = News.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      News.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

exports.get_company_information = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = CompanyInformation.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      CompanyInformation.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};
//获取新闻详情
exports.get_news_detail = function(req, res) {

  var id = req.body.id;
  News.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个新闻
exports.del_one = function(req, res) {
  var id = req.body.id;
  News.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });

};

//获取企业新闻详情
exports.get_company_detail = function(req, res) {

  var id = req.body.id;
  CompanyInformation.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};

//删除一个企业新闻
exports.company_del_one = function(req, res) {
  var id = req.body.id;
  CompanyInformation.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });

};

// 添加离职公示
exports.add_quit = function(req, res) {
  var name = req.body.name;
  var time = req.body.time;
  var department = req.body.department;

  var data = new Quit({
    name: name,
    time: time,
    department: department
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 分页获取离职公示
exports.get_quits = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Quit.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Quit.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};
//删除一个离职公告
exports.del_quit = function(req, res) {
  var id = req.body.id;
  Quit.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });

};

// 进入登录页面
exports.login = function(req, res) {
  res.render('admin/login');
};
// 进入登录页面
exports.register = function(req, res) {
  res.render('admin/register');
};

// post登录信息，校验
exports.checkUser = function(req, res) {
  var username = req.body.username;
  var password = req.body.password;
  var captcha = req.body.captcha;

  if(captcha != req.session.captcha) {
    console.log('captcha error');
    res.json({
      'status': 'captcha error'
    });
  } else { //验证码正确
    User.findOne({
      username: username
    }, function(err, doc) {
      if(err) {
        console.log('error');
        res.json({
          'status': 'error'
        });
      } else if(doc == null) {
        console.log('not exist');
        res.json({
          'status': 'not exist'
        });
      } else if(doc.status === '0') {
        //如果是刚注册的用户，还未授权无法登陆。
        console.log('unchecked');
        res.json({
          'status': 'unchecked'
        });
      } else if(doc.password === password) {
        console.log('success');
        //登录成功，将user保存到session中
        req.session.user = doc;
        res.json({
          'status': 'success'
        });
      } else {
        console.log('password error');
        res.json({
          'status': 'password error'
        });
      }
    });
  }
};
//用户登出操作
exports.logout = function(req, res) {
  req.session.user = null;
  res.redirect('/login');
};

//添加管理员
exports.post_register = function(req, res) {
  var username = req.body.username;
  var password = req.body.password;
  var realname = req.body.realname;
  var phone = req.body.phone;
  var captcha = req.body.captcha;

  if(captcha != req.session.captcha) {
    console.log('captcha error');
    res.json({
      'status': 'captcha error'
    });
  } else { //验证码正确
    var user = new User({
      username: username,
      password: password,
      realname: realname,
      phone: phone,
      status: '0'
    });
    User.findOne({
      username: username
    }, function(err, doc) {
      if(err) {
        res.json({
          'status': 'error'
        });
      } else if(doc) {
        res.json({
          'status': 'user exist'
        });
      } else {
        User.create(user, function(err, doc) {
          if(err) {
            res.json({
              'status': 'error'
            });
          }
          res.json({
            'status': 'success'
          });
        });
      }
    });
  }

};

//确定授权
exports.authorize = function(req, res) {

  if(req.session.user.status != '2') {
    //0表示普通用户，1表示已授权普通用户，2表示高级用户
    res.json({
      "status": "noRight"
    });
  } else {
    var id = req.body.id;
    User.findOne({
      _id: id
    }, function(err, doc) {
      if(err) {
        res.json({
          "status": "error"
        });
      } else {
        doc.status = '1';
        //修改后必须的保存
        doc.save(function(err) {
          if(err) {
            res.json({
              "status": "error"
            })
          } else {
            res.json({
              "status": "success"
            });
          }
        })
      }
    });
  }
};

//添加管理员（用于postMan测试，可删除）
exports.add_admin = function(req, res) {
  if(req.session.user.status != '2') {
    //0表示普通用户，1表示已授权普通用户，2表示高级用户
    res.json({
      "status": "noRight"
    });
  } else {
    //超级管理员有权利删除操作
    var username = req.body.username;
    var password = req.body.password;
    var realname = req.body.realname;
    var phone = req.body.phone;

    var user = new User({
      username: username,
      password: password,
      realname: realname,
      phone: phone
    });

    User.findOne({
      username: username
    }, function(err, doc) {
      if(err) {
        res.json({
          'status': 'error'
        });
      } else if(doc) {
        res.json({
          'status': 'user_exist'
        });
      } else {
        User.create(user, function(err, doc) {
          if(err) {
            res.json({
              'status': 'error'
            });
          }
          res.json({
            'status': 'success'
          });
        });
      }
    });
  }
};
// 分页获取离职公示
exports.get_users = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = User.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      User.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};
//删除一个管理员
exports.user_del = function(req, res) {

  if(req.session.user.status != '2') {
    //0表示普通用户，1表示已授权普通用户，2表示高级用户
    res.json({
      "status": "noRight"
    });
  } else {
    var id = req.body.id;
    User.remove({
      _id: id
    }, function(err, doc) {
      if(err) {
        res.json({
          "status": "error"
        });
      } else {
        res.json({
          "status": "success"
        })
      }
    });
  }
};

// 分页获取离职公示
exports.get_feedbacks = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Feedback.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Feedback.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//通过_id反馈信息
exports.deal_feedbacks = function(req, res) {
  var id = req.body.id;
  Feedback.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      doc.status = '1';
      //修改后必须的保存
      doc.save(function(err) {
        if(err) {
          res.json({
            "status": "error"
          })
        } else {
          res.json({
            "status": "success"
          });
        }
      })
    }
  });
};

//校验后台是否有未处理反馈信息
exports.check_feedbacks = function(req, res) {
  //计算未处理反馈信息条数
  Feedback.find({
    'status': '0'
  }, function(err, result) {
    if(err) {
      res.json({
        'status': 'error'
      });
    } else if(result.length === 0) {
      res.json({
        'status': 'error'
      });
    } else {
      res.json({
        'status': 'success',
        'size': result.length
      });
    }
  });
};

//荣誉
exports.add_honor1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_honor', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.edit_honor1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  Honor.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_honor', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};

exports.honor_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/honor_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

// 添加新闻
exports.add_honor = function(req, res) {
  var title = req.body.title;
  var time = req.body.time;
  var category = req.body.category;
  var content = req.body.content;

  var data = new Honor({
    title: title,
    time: time,
    category: category,
    content: content
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 编辑荣誉
exports.edit_honor = function(req, res) {
  var id = req.body._id;
  var title = req.body.title;
  var category = req.body.category;
  var content = req.body.content;
  var data = {
    title: title,
    category: category,
    content: content
  }
  Honor.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};

// 分页获取荣誉
exports.get_honor = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Honor.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Honor.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//获取荣誉详情
exports.get_honor_detail = function(req, res) {

  var id = req.body.id;
  Honor.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个荣誉
exports.honor_del_one = function(req, res) {
  var id = req.body.id;
  Honor.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });
}

//证书
exports.add_certificate1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_certificate', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.edit_certificate1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  Certificate.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_certificate', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};

exports.certificate_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/certificate_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

// 添加新闻
exports.add_certificate = function(req, res) {
  var title = req.body.title;
  var time = req.body.time;
  var category = req.body.category;
  var content = req.body.content;
  var secondTitle = req.body.secondTitle;
  var img = req.body.img;
  var sort = req.body.sort;
  var toLocation = req.body.toLocation;
  console.log(toLocation+"1234")
  var data = new Certificate({
    title: title,
    time: time,
    category: category,
    content: content,
    secondTitle:secondTitle,
    img:img,
    sort:sort,
    toLocation:toLocation
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 编辑证书
exports.edit_certificate = function(req, res) {
  var id = req.body._id;
  var title = req.body.title;
  var category = req.body.category;
  var content = req.body.content;
  var secondTitle = req.body.secondTitle;
  var sort = req.body.sort;
  var toLocation = req.body.toLocation;
  var img = req.body.img;
  var data = {
    title: title,
    category: category,
    content: content,
    secondTitle:secondTitle,
    img:img,
    sort:sort,
    toLocation:toLocation
  }
  Certificate.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};

// 分页获取证书
exports.get_certificate = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Certificate.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Certificate.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//获取证书详情
exports.get_certificate_detail = function(req, res) {

  var id = req.body.id;
  Certificate.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个证书
exports.certificate_del_one = function(req, res) {
  var id = req.body.id;
  Certificate.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });
}
//招聘
exports.add_recruit1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_recruit', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.edit_recruit1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  Recruit.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_recruit', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};

exports.recruit_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/recruit_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

// 添加新闻
exports.add_recruit = function(req, res) {
  var position = req.body.position;
  var time = req.body.time;
  var population = req.body.population; //招聘人数
  var place = req.body.place; //工作地点
  var sex = req.body.sex; //性别
  var age = req.body.age; //年龄
  var seniority = req.body.seniority; //工龄
  var educational = req.body.educational; //学历
  var treatment = req.body.treatment; //学历
  var language = req.body.language; //语言
  //获取多文本编辑内容
  var content = req.body.content;
  var data = new Recruit({
    position: position,
    time: time,
    population: population,
    place: place,
    sex: sex,
    age: age,
    seniority: seniority,
    educational: educational,
    language: language,
    treatment: treatment,
    content: content
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 编辑荣誉
exports.edit_recruit = function(req, res) {
  var id = req.body._id;
  var position = req.body.position;
  var population = req.body.population; //招聘人数
  var place = req.body.place; //工作地点
  var sex = req.body.sex; //性别
  var age = req.body.age; //年龄
  var seniority = req.body.seniority; //工龄
  var educational = req.body.educational; //学历
  var treatment = req.body.treatment; //学历
  var language = req.body.language; //语言
  //获取多文本编辑内容
  var content = req.body.content;
  var data = {
    position: position,
    population: population,
    place: place,
    sex: sex,
    age: age,
    seniority: seniority,
    educational: educational,
    language: language,
    treatment: treatment,
    content: content
  }
  Recruit.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};

// 分页获取荣誉
exports.get_recruit = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Recruit.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Recruit.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//获取荣誉详情
exports.get_recruit_detail = function(req, res) {

  var id = req.body.id;
  Recruit.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个荣誉
exports.recruit_del_one = function(req, res) {
  var id = req.body.id;
  Recruit.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });

};

exports.apply_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/apply_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

// 分页获取荣誉
exports.get_apply = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Apply.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Apply.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//获取荣誉详情
exports.get_apply_detail = function(req, res) {

  var id = req.body.id;
  Apply.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个荣誉
exports.apply_del_one = function(req, res) {
  var id = req.body.id;
  Apply.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });

};

//荣誉
exports.add_cultrue1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_cultrue', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.edit_cultrue1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  Cultrue.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_cultrue', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};

exports.cultrue_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/cultrue_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

// 添加新闻
exports.add_cultrue = function(req, res) {
  var title = req.body.title;
  var time = req.body.time;
  var category = req.body.category;
  var content = req.body.content;

  var data = new Cultrue({
    title: title,
    time: time,
    category: category,
    content: content
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 编辑荣誉
exports.edit_cultrue = function(req, res) {
  var id = req.body._id;
  var title = req.body.title;
  var category = req.body.category;
  var content = req.body.content;
  var data = {
    title: title,
    category: category,
    content: content
  }
  Cultrue.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};

// 分页获取荣誉
exports.get_cultrue = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Cultrue.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Cultrue.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//获取荣誉详情
exports.get_cultrue_detail = function(req, res) {

  var id = req.body.id;
  Cultrue.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个荣誉
exports.cultrue_del_one = function(req, res) {
  var id = req.body.id;
  Cultrue.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });
}

//base64转成图片
exports.base64ToImg = function(req, res) {
  console.log(req.body.img)
  var dataBuffer =new Buffer(req.body.img, 'base64')
    var timestamp = Date.parse(new Date()).toString();
    if(!fs.existsSync("public/web/images/upload/certificate/")) {
      fs.mkdir("public/web/images/upload/certificate/");
    }
    
    fs.writeFile("public/web/images/upload/certificate/" + timestamp + ".png", dataBuffer, function(err) {

        if(err) {
        res.json("error")
      } else {
        res.json("/web/images/upload/certificate/" + timestamp + ".png")
      }

    });

//var id = req.body.id;
//Cultrue.remove({
//  _id: id
//}, function(err, doc) {
//  if(err) {
//    res.json({
//      "status": "error"
//    });
//  } else {
//    res.json({
//      "status": "success"
//    })
//  }
//});
}

//首页外链
exports.add_home1 = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/add_home', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

exports.edit_home1 = function(req, res) {
  var id = req.params.id;
  var isSuper = '普通管理员'
  var host = req.get('host')
  var content = '';
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  Home.findOne({
    _id: id
  }, function(err, data) {
    if(err) {
      data = "获取数据失败"
    } else {
      res.render('admin/edit_home', {
        username: req.session.user.username,
        isSuper: isSuper,
        host: host,
        data: data,
        id: id
      });
    }
  })

};

exports.home_list = function(req, res) {
  var isSuper = '普通管理员'
  var host = req.get('host')
  if(req.session.user.status === '2') {
    isSuper = '超级管理员'
  }
  res.render('admin/home_list', {
    username: req.session.user.username,
    isSuper: isSuper,
    host: host
  });
};

// 添加新闻
exports.add_home = function(req, res) {
  var title = req.body.title;
  var time = req.body.time;
  var category = req.body.category;
  var typeContent = req.body.typeContent;
  var secondTitle = req.body.secondTitle;
  var img = req.body.img;
  var sort = req.body.sort;
  var toLocation = req.body.toLocation;
  var data = new Home({
    title: title,
    time: time,
    category: category,
    secondTitle:secondTitle,
    img:img,
    sort:sort,
    toLocation:toLocation,
    typeContent:typeContent
  });
  data.save(function(err) {
    if(err) {
      res.json({
        "status": "error"
      })
    } else {
      res.json({
        "status": "success"
      });
    }
  });
};
// 编辑证书
exports.edit_home = function(req, res) {
  var id = req.body._id;
  var title = req.body.title;
  var category = req.body.category;
  var typeContent = req.body.typeContent;
  var secondTitle = req.body.secondTitle;
  var sort = req.body.sort;
  var toLocation = req.body.toLocation;
  var img = req.body.img;
  var data = {
    title: title,
    category: category,
    typeContent: typeContent,
    secondTitle:secondTitle,
    img:img,
    sort:sort,
    toLocation:toLocation
  }
  Home.findByIdAndUpdate({
    _id: id
  }, data, {
    multi: true
  }, function(err, docs) {
    if(err) {
      console.log(err);
      res.json({
        "status": "failed"
      });
    } else {
      res.json({
        "status": "success"
      });
    }
  })
};

// 分页获取证书
exports.get_home = function(req, res) {
  var curr = req.body.curr;
  //每页大小为10
  var query = Home.find({});
  query.skip((curr - 1) * 10);
  query.limit(10);
  //按照id添加的顺序倒序排列
  query.sort({
    '_id': -1
  });
  //计算分页数据
  query.exec(function(err, rs) {
    if(err) {
      res.send(err);
    } else {
      //计算数据总数
      Home.find(function(err, result) {
        if(result.length % 10 > 0) {
          pages = result.length / 10 + 1;
        } else {
          pages = result.length / 10;
        }
        jsonArray = {
          data: rs,
          pages: pages
        };
        res.json(jsonArray);
      });
    }
  });
};

//获取证书详情
exports.get_home_detail = function(req, res) {

  var id = req.body.id;
  Home.findOne({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success",
        "data": doc.content
      })
    }
  });
};
//删除一个证书
exports.home_del_one = function(req, res) {
  var id = req.body.id;
  Home.remove({
    _id: id
  }, function(err, doc) {
    if(err) {
      res.json({
        "status": "error"
      });
    } else {
      res.json({
        "status": "success"
      })
    }
  });
}